package com.yjk.dzpoke.websocket.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Poke implements Comparable<Poke> {
    /**
     * 1黑桃♠ 2红桃♥ 3方块♦ 4梅花♣
     */
    private Integer color;
    /**
     * 2-10 J=11 Q=12 K=13 A=14
     */
    private Integer point;

    /**
     * 获取一副打乱的扑克
     *
     * @return
     */
    public static List<Poke> NewPokes() {
        List<Poke> list = new ArrayList<>();
        for (int i = 2; i <= 14; i++) {
            for (int j = 1; j <= 4; j++) {
                list.add(new Poke(j, i));
            }
        }
        Collections.shuffle(list);
        return list;
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        switch (color) {
            case 1:
                sb.append("黑桃♠");
                break;
            case 2:
                sb.append("红桃♥");
                break;
            case 3:
                sb.append("方块♦");
                break;
            case 4:
                sb.append("梅花♣");
                break;
        }
        if (point >= 2 && point <= 10) {
            sb.append(point);
        } else {
            switch (point) {
                case 11:
                    sb.append("J");
                    break;
                case 12:
                    sb.append("Q");
                    break;
                case 13:
                    sb.append("K");
                    break;
                case 14:
                    sb.append("A");
                    break;
            }
        }
        return sb.toString();
    }

    /**
     * 抽取一定数量的扑克
     *
     * @param pokes
     * @param count
     * @return
     */
    public static List<Poke> getPoke(List<Poke> pokes, Integer count) {
        List<Poke> list = new ArrayList<>();
        Iterator<Poke> it = pokes.iterator();
        int i = 0;
        while (it.hasNext() && i < count) {
            Poke poke = it.next();
            list.add(poke);
            it.remove();
            i++;
        }
        return list;
    }

    public static void main(String[] args) {
        List<Poke> list = NewPokes();
        while (!list.isEmpty()) {
            List<Poke> get = getPoke(list, 4);
            for (Poke poke : get) {
                System.out.println(poke);
            }
            System.out.println("抽取");
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Poke poke = (Poke) o;
        return Objects.equals(color, poke.color) && Objects.equals(point, poke.point);
    }

    @Override
    public int hashCode() {
        return Objects.hash(color, point);
    }

    @Override
    public int compareTo(Poke that) {
        if (this.color == that.color)
            return this.point - that.point;
        else return this.color - that.color;
    }
}
